package com.baide.xguangji.swing;

import com.baide.xguangji.plc.PlcCallBack;
import com.baide.xguangji.plc.PlcSendMsg;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

@Component
public class MainContent extends JFrame implements PlcCallBack {

    PlcSendMsg plcSendMsg;

    String status = "未启动";//设备默认未启动


    public void setPlcSendMsg(PlcSendMsg plcSendMsg) {
        this.plcSendMsg = plcSendMsg;
    }

    public MainContent() {
        initContent();
    }

    private void initContent() {
        setTitle("百德海关查验三关合一系统");
//        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dispose();
        setBounds(200, 50, 900, 600);
        add(getNorth(), BorderLayout.NORTH);
        add(getCenter());
        setVisible(true);
    }

    List<Map<String, Object>> data;
    TableModel tableModel;

    private JPanel getCenter() {
        JPanel table = new JPanel();
        table.setLayout(new BorderLayout());
        data = new ArrayList<>();
        tableModel = new TableModel(data);
        JTable table1 = new JTable(tableModel);
        JScrollPane js = new JScrollPane(table1);
        table.add(js);
        return table;
    }

    private JPanel getNorth() {
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout());
        JPanel center = new JPanel();
        center.setLayout(new FlowLayout());
        center.add(getConnectPanel());
        jPanel.add(center);
        jPanel.add(codePanel(), BorderLayout.SOUTH);
        return jPanel;
    }

    //扫码枪对应的控件
    private JPanel codePanel() {
        JPanel jPanel = new JPanel();
        JLabel jLabel = new JLabel("扫描申报条码:");
        JTextField jTextField = new JFormattedTextField();
        jTextField.setColumns(20);
        jPanel.add(jLabel);
        jPanel.add(jTextField);
        JLabel jLabel1 = new JLabel("tip:请将点击获取鼠标焦点，扫码枪自动录入");
        jLabel1.setForeground(Color.RED);
        jPanel.add(jLabel1);
        return jPanel;
    }


    private JPanel getConnectPanel() {
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        JPanel center = new JPanel();
        TitledBorder titledBorder = BorderFactory.createTitledBorder("连接信息");
        titledBorder.setTitleJustification(TitledBorder.TOP);
        center.setBorder(titledBorder);
        center.setLayout(new GridLayout(2, 2));//设置网格布局
        setConnectLabel(center);
        jPanel.add(center);
        jPanel.add(getButton(), BorderLayout.EAST);
        return jPanel;
    }

    String[] connectStrings = new String[]{"X光机:", "PLC通讯:", "扫码枪:", "设备运行:"};
    JLabel[] jLabels = new JLabel[4];
    JPanel[] jPanels = new JPanel[4];

    private void setConnectLabel(JPanel jPanel) {
        for (int i = 0; i < connectStrings.length; i++) {
            JPanel panel = new JPanel();
            JLabel label = new JLabel(connectStrings[i]);
            JPanel j = new JPanel();
            j.setPreferredSize(new Dimension(70, 20));
            j.add(label);
            JLabel label1 = new JLabel("未连接");
            jLabels[i] = label1;
            JPanel j1 = new JPanel();
            j1.setPreferredSize(new Dimension(70, 20));
            j1.setBackground(Color.RED);
            if (i == 2 ) {
                label1.setText("已就绪");
                j1.setBackground(Color.GREEN);
            }
            if (i == 3) {
                label1.setText(status);
            }
            j1.add(label1);
            jPanels[i] = j1;
            panel.add(j);
            panel.add(j1);
            jPanel.add(panel);
        }
    }

    private JPanel getButton() {
        JPanel jPanel = new JPanel();
        JPanel j = new JPanel();
        j.setLayout(new GridLayout(1, 2, 5, 0));
        JButton start = new JButton("启动");
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                plcSendMsg.line2Stop(1);
                jLabels[3].setText("已启动");
                jPanels[3].setBackground(Color.GREEN);
            }
        });
        JButton stop = new JButton("停止");
        stop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                plcSendMsg.line2Stop(0);
                jLabels[3].setText("未启动");
                jPanels[3].setBackground(Color.RED);
            }
        });
        j.add(start);
        j.add(stop);
        jPanel.add(j);
        return jPanel;
    }


    private void changeUI(int i, String msg, Color color) {
        //回到主线程更新UI
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                jLabels[i].setText(msg);
                jPanels[i].setBackground(color);
            }
        });
    }


    @Override
    public void PLCConnectSuccess() {
        changeUI(2, "已连接", Color.GREEN);
    }

    @Override
    public void PLCConnectOff() {
        changeUI(2, "未连接", Color.RED);
    }

    @Override
    public void light(int i) {
        switch (i) {
            case 1:
                break;
        }
    }

}
